package com.shixun.dds_11.controller;


import com.shixun.dds_11.context.BaseContext;
import com.shixun.dds_11.dto.JbuyDTO;
import com.shixun.dds_11.dto.OrderPageQueryDTO;
import com.shixun.dds_11.dto.OrderSubmitDTO;
import com.shixun.dds_11.dto.OrdersInfoDTO;
import com.shixun.dds_11.entity.DdsOrder;
import com.shixun.dds_11.result.PageResult;
import com.shixun.dds_11.result.Result;
import com.shixun.dds_11.service.IDdsOrderService;
import com.shixun.dds_11.service.IUserService;
import com.shixun.dds_11.vo.OrdersInfoVO;
import com.shixun.dds_11.vo.UserQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author SSL
 * @since 2024-07-05
 */
@CrossOrigin
@RestController
@RequestMapping("/dds11/order")
@Slf4j
@Api(tags = "订单相关接口")
public class DdsOrderController {

    @Autowired
    private IDdsOrderService iDdsOrderService;

    @Autowired
    IUserService iUserService;


    @PostMapping("/jbuy")
    @ApiOperation("立即购买")
    public Result<Integer> Jbuy(@RequestBody JbuyDTO jbuyDTO){
        log.info("立即购买");
        return Result.success(iDdsOrderService.insert(jbuyDTO));

    }

    /**
     * 订单列表查询
     * @return
     */
    @GetMapping("/{page}")
    @ApiOperation(value = "查询订单列表")
    public Result<PageResult> orderQuery(@PathVariable Integer page) {
        log.info("查询订单列表");
        PageResult pageResult = iDdsOrderService.getList(page);
        return Result.success(pageResult);
    }


    /**
     * 查询订单详情
     * @return
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "查询订单详情")
    public Result<PageResult> info(@PathVariable Integer id) {
        log.info("查询订单详情");

        return Result.success(iDdsOrderService.detail(id));
    }


    /**
     * 提交订单
     * @return
     */
    @PostMapping("/submit/{id}")
    @ApiOperation(value = "提交订单接口")
    public Result submit(@PathVariable Integer id) {
        log.info("提交订单");
        iDdsOrderService.submit(id);
        return Result.success();
    }


    /**
     * 用于显示用户地址
     * @return
     */
    @ApiOperation(value = "用于显示用户地址")
    @GetMapping("/query")
    public Result<UserQueryVO> query() {
        Integer userId = BaseContext.getCurrentId().intValue();
        UserQueryVO userQueryVO = iUserService.getByuserId(userId);
        return Result.success(userQueryVO);
    }


}
